import os
import sys
path='.'
for file in os.listdir(path):
	if file.startswith('bank'):
		if not file.startswith('bankGND'):
			if file.endswith('.txt'):
				name=file[4:-4]
				f=open('bank'+name+'.txt')
				lines=f.readlines()
				f.close()
				with open('bank'+name+'.symdef','aw') as ff:
					ff.write('[labels]'+'\n')
					ff.write('BANK '+name+'\n')
					ff.write('refdes=U?'+'\n')
					ff.write('XC7VX485TFFG1761'+'\n')
					ff.write('! author=Yilun Xu'+'\n')
					ff.write('! device='+'\n')
					ff.write('! footprint='+'\n')
					ff.write('\n')
				with open('bank'+name+'.symdef','aw') as ff:
					ff.write('[left]'+'\n')
					for i in range(len(lines)):
						if lines[i].split()[1][:4]=='VCCO':
							ff.write(lines[i])
					ff.write('\n')
				with open('bank'+name+'.symdef','aw') as ff:
					ff.write('[right]'+'\n')
					for i in range(len(lines)):
						if lines[i].split()[1][:4]!='VCCO':
							ff.write(lines[i])
		if file=='bankGND1.txt':
			name=file[4:-4]
			f=open('bank'+name+'.txt')
			lines=f.readlines()
			f.close()
			for i in range(len(lines)):
				if lines[i].split()[0]=='A2':
					index1=i
				if lines[i].split()[0]=='AG25':
					index2=i
				if lines[i].split()[0]=='AG27':
					index3=i
				if lines[i].split()[0]=='AY5':
					index4=i
			with open('bank'+name+'.symdef','aw') as ff:
				ff.write('[labels]'+'\n')
				ff.write('BANK '+name+'\n')
				ff.write('refdes=U?'+'\n')
				ff.write('XC7VX485TFFG1761'+'\n')
				ff.write('! author=Yilun Xu'+'\n')
				ff.write('! device='+'\n')
				ff.write('! footprint='+'\n')
				ff.write('\n')
			with open('bank'+name+'.symdef','aw') as ff:
				ff.write('[left]'+'\n')
				for i in range(len(lines)):
					if index3<=i<=index4:
						ff.write(lines[i])
				ff.write('\n')
			with open('bank'+name+'.symdef','aw') as ff:
				ff.write('[right]'+'\n')
				for i in range(len(lines)):
					if index1<=i<=index2:
						ff.write(lines[i])
		if file=='bankGND2.txt':
			name=file[4:-4]
			f=open('bank'+name+'.txt')
			lines=f.readlines()
			f.close()
			for i in range(len(lines)):
				if lines[i].split()[0]=='AY9':
					index1=i
				if lines[i].split()[0]=='M15':
					index2=i
				if lines[i].split()[0]=='M25':
					index3=i
				if lines[i].split()[0]=='Y41':
					index4=i
			with open('bank'+name+'.symdef','aw') as ff:
				ff.write('[labels]'+'\n')
				ff.write('BANK '+name+'\n')
				ff.write('refdes=U?'+'\n')
				ff.write('XC7VX485TFFG1761'+'\n')
				ff.write('! author=Yilun Xu'+'\n')
				ff.write('! device='+'\n')
				ff.write('! footprint='+'\n')
				ff.write('\n')
			with open('bank'+name+'.symdef','aw') as ff:
				ff.write('[left]'+'\n')
				for i in range(len(lines)):
					if index3<=i<=index4:
						ff.write(lines[i])
				ff.write('\n')
			with open('bank'+name+'.symdef','aw') as ff:
				ff.write('[right]'+'\n')
				for i in range(len(lines)):
					if index1<=i<=index2:
						ff.write(lines[i])

for file in os.listdir(path):
	if file.endswith('.symdef'):
		os.system('make %s' %(file[:-3]))
